package com.eqdd.yiqidian.ui.gongneng.renshi.zuzhiguanli;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.eqdd.yiqidian.ItemEnterCustom;
import com.eqdd.yiqidian.LinearlayoutCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.ZuzhiJiagouSetActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SectionArcBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.listener.OnMyLongClickListener;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.eqdd.yiqidian.widget.EditDialogFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import java.util.List;

public class ZuzhiJiagouSetActivity extends BaseActivity {
    private ZuzhiJiagouSetActivityCustom dataBinding;
    private EditDialogFragment editDiglog;
    private ActionSheetDialog bottomChoose;
    private BaseQuickAdapter<SectionArcBean, BaseViewHolder> quickAdapter;
    private MVCSwipeRefreshHelper<List<SectionArcBean>> mvcHelper;
    private List<SectionArcBean> sections;
    private String pparent;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_zuzhi_jiagou_set);
        initTopTitleBar(View.VISIBLE, "组织设置");
        initTopRightText("新增", (v) -> {
            pparent=user.getComid();
            bottomChoose.show();
        });
    }

    @Override
    public void initData() {
    }

    @Override
    public void setView() {
        initChooseDialog();
        refreshView();
    }

    private void refreshView() {
        new HttpPresneter.Builder<String>(HttpConfig.GET_SECTION_ARCHITECTURE, String.class, this)
                .Params("personid", user.getComid())
                .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                    @Override
                    public void onError(Throwable e) {
                        System.out.println(e.toString());
                    }

                    @Override
                    public void onNext(Object o) {
                        sections = new Gson().fromJson((String) o, new TypeToken<List<SectionArcBean>>() {
                        }.getType());
                        dataBinding.llRoot.removeAllViews();
                        updataView(sections, 0,dataBinding.llRoot);
                    }
                })
                .build()
                .post();
    }

    private void updataView(List<SectionArcBean> list, int padding,LinearLayout root) {
        if (list != null) {
            for (SectionArcBean sectionArcBean : list) {
                ItemEnterCustom inflate = DataBindingUtil.inflate(getLayoutInflater(), R.layout.item_enter, null, false);
                inflate.getRoot().setPadding(padding, 0, 0, 0);
                inflate.tvContent.setText(sectionArcBean.getDname());
                inflate.getRoot().setOnLongClickListener(new OnMyLongClickListener(sectionArcBean) {
                    @Override
                    public boolean onLongClick(View v) {
                        pparent = ((SectionArcBean) getObject()).getId()+"";
                        bottomChoose.show();
                        return true;
                    }
                });
                root.addView(inflate.getRoot());
                if (sectionArcBean.getChilds()!=null) {
                    LinearlayoutCustom llinflate = DataBindingUtil.inflate(getLayoutInflater(), R.layout.item_linearlayout, null, false);
                    updataView(sectionArcBean.getChilds(), padding + 50, (LinearLayout) llinflate.getRoot());
                    root.addView(llinflate.getRoot());
                    inflate.getRoot().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            llinflate.getRoot().setVisibility(llinflate.getRoot().getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE);
                        }
                    });
                }

            }
        }
    }


    private void initChooseDialog() {
        bottomChoose = ChooseAlertView.getBottomChoose(this, null, new String[]{"新增部门", "删除部门", "修改","新增岗位"}, (v, p) -> {
            switch (p) {
                case 1:
                        editDiglog = ChooseAlertView.getEditDiglog("部门名称","部门名称","部门代码", (content) -> {
                            new HttpPresneter.Builder<String>(HttpConfig.CREATE_SECTION, String.class, ZuzhiJiagouSetActivity.this)
                                    .Params("personid", user.getComid())
                                    .Params("postname", content.get(0))
                                    .Params("pparent", TextUtils.isEmpty(pparent) ? user.getComid() : pparent)
                                    .Params("pdescrib", content.get(1))
                                    .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                                        @Override
                                        public void onError(Throwable e) {
                                            ToastUtil.showLong(Config.SERVER_ERROR);
                                        }
                                        @Override
                                        public void onNext(Object o) {
                                            refreshView();
                                        }
                                    })
                                    .build()
                                    .post();
                        });
                    editDiglog.show(getSupportFragmentManager(), "edit");
                    break;
                case 2:
                    new HttpPresneter.Builder<String>(HttpConfig.DELETE_SECTION, String.class, ZuzhiJiagouSetActivity.this)
                            .Params("pid", TextUtils.isEmpty(pparent) ? "0" : pparent)
                            .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                                @Override
                                public void onError(Throwable e) {
                                    ToastUtil.showLong(Config.SERVER_ERROR);
                                }
                                @Override
                                public void onNext(Object o) {
                                    refreshView();
                                }
                            })
                            .build()
                            .post();
                    break;
                case 3:
                    editDiglog = ChooseAlertView.getEditDiglog("部门名称",null,null, (content) -> {
                        new HttpPresneter.Builder<String>(HttpConfig.UPDATE_SECTION, String.class, ZuzhiJiagouSetActivity.this)
                                .Params("pname", content.get(0))
                                .Params("pid", TextUtils.isEmpty(pparent) ? user.getComid() : pparent)
                                .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                                    @Override
                                    public void onError(Throwable e) {
                                        ToastUtil.showLong(Config.SERVER_ERROR);
                                    }
                                    @Override
                                    public void onNext(Object o) {
                                        refreshView();
                                    }
                                })
                                .build()
                                .post();
                    });
                    editDiglog.show(getSupportFragmentManager(), "edit");
                    break;
                case 4:
                    editDiglog = ChooseAlertView.getEditDiglog("岗位名称",null,null, (content) -> {
                        System.out.println(content);
                        new HttpPresneter.Builder<String>(HttpConfig.CREATE_SECTION, String.class, ZuzhiJiagouSetActivity.this)
                                .Params("personid", user.getComid())
                                .Params("postname", content.get(0))
                                .Params("pparent", TextUtils.isEmpty(pparent) ? user.getComid() : pparent)
                                .Params("pdescrib", "")
                                .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                                    @Override
                                    public void onError(Throwable e) {
                                        ToastUtil.showLong(Config.SERVER_ERROR);
                                    }
                                    @Override
                                    public void onNext(Object o) {
                                        refreshView();
                                    }
                                })
                                .build()
                                .post();
                    });
                    editDiglog.show(getSupportFragmentManager(), "edit");
                    break;
                case 5:
                    new HttpPresneter.Builder<String>(HttpConfig.SECTION_ADD_PERSON,String.class,this)
                            .Params("uid",user.getUname())
                            .Params("cid",user.getComid())
                            .Params("csec","")
                            .Params("cpost","")
                            .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(Object o) {

                                }
                            })
                            .build()
                            .post();
                    break;
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
